package com.webdesign.util;
import java.awt.AWTException;
import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.List;

import javax.imageio.ImageIO;

/*
 * 根据网页url截图
 */
public class CutPictureUtil {
	public String haveImage(String url) throws MalformedURLException,
			IOException, URISyntaxException, AWTException {
		//打开网页
		Desktop.getDesktop().browse(new URL(url).toURI());
		Robot robot = new Robot();
		String root = this.getClass().getResource("/").getFile().toString();
		String rt[] = root.split("/WEB-INF");
		robot.delay(2000);
		//确定屏幕大小
		Dimension d = new Dimension(Toolkit.getDefaultToolkit().getScreenSize());
		int width = (int) d.getWidth();
		int height = (int) d.getHeight();
		// 最大化浏览器
		robot.keyRelease(KeyEvent.VK_F11);
		robot.delay(5000);
		//截屏，0,0是坐标位置
		Image image = robot.createScreenCapture(new Rectangle(0, 87, width,700));
		BufferedImage bi = new BufferedImage(width, 700,BufferedImage.TYPE_INT_RGB);
		Graphics g = bi.createGraphics();
		g.drawImage(image, 0, 0, width, height, null);
		// 根据UUID获取随机序列号，以此命名保存图片
		String uid = UUIDUtil.getUUid();
		String imgaddr = rt[0]+ "/img/"+uid+".jpg";
		ImageIO.write(bi, "jpg", new File(imgaddr));
		return  "http://localhost:8080/WebDesign/img/"+uid+".jpg";
	}
}